package cs342.uwosh.edu.QuizApp;


import cs342.uwosh.edu.R;
import cs342.uwosh.edu.QuizApp.database.Encryption;
import cs342.uwosh.edu.QuizApp.storage.Question;
import cs342.uwosh.edu.QuizApp.storage.Quiz;
import cs342.uwosh.edu.QuizApp.thread.AsyncMethod;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class TakeQuizActivity extends Activity {

	
	private Quiz quiz;
	private Question currentQuestion;
	private String username;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.question);
		
		Intent i = getIntent();
		username = i.getStringExtra("user");
		
		quiz = QuizListActivity.quiz;
		currentQuestion = quiz.getCurrentQuestion();
		
		((TextView)(findViewById(R.id.questionText))).setText(currentQuestion.getDescription());
		currentQuestion.display(this, (LinearLayout)(findViewById(R.id.questionBody)));
		updateStatuses();
	}
	
	public void next(View view) {
		currentQuestion.checkAnswer();
		if(currentQuestion.isCorrect()) {
			Toast.makeText(getBaseContext(),"Correct", Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(getBaseContext(),"The correct answer is: " + currentQuestion.getAnswer(), Toast.LENGTH_LONG).show();
		}
		if(quiz.isOnLastQuestion()) {
			recordAnswer(quiz.getName(), quiz.getScore(), quiz.getQuestionCount());
			super.onBackPressed();
		} else {
			quiz.nextQuestion();
			refreshActivity();
		}
	}
	
	public void show(View view) {
		currentQuestion.showAnswer();
		refreshActivity();
	}
	
	public void previous(View view) {
		currentQuestion.checkAnswer();
		if(quiz.isOnFirstQuestion()) {
			super.onBackPressed();
		} else {
			quiz.previousQuestion();
			refreshActivity();
		}
	}
	
	public void updateStatuses() {
		
		// Update Current Score and Question Number Text
		int numQuestion = quiz.getQuestionCount();
		int score = quiz.getScore();
		
		((TextView)(findViewById(R.id.score))).setText("Score: " + score + "/" + numQuestion + "\n");
		((TextView)(findViewById(R.id.questionNum))).setText(quiz.getQuestionNumber() + ". ");
		
		// Update Button Text
		Button next, show, previous;
		next = (Button)(findViewById(R.id.nextButton));
		//show = (Button)(findViewById(R.id.showButton));
		//previous = (Button)(findViewById(R.id.previousButton));
		
		//show.setText(R.string.show);
		if(quiz.isOnFirstQuestion()) {
			next.setText(R.string.next);
			//previous.setText(R.string.exit);
		} else if(quiz.isOnLastQuestion()){
			next.setText(R.string.finish);
			//previous.setText(R.string.previous);
		} else {
			next.setText(R.string.next);
			//previous.setText(R.string.previous);
		}
	}
	
	public void refreshActivity() {
		((LinearLayout)(findViewById(R.id.questionBody))).removeAllViews();
		Intent i = new Intent(this, TakeQuizActivity.class);
		i.putExtra("user", username);
		startActivity(i);
		finish();
	}
	
	public void recordAnswer(String quizname, int score, int total) {
		Encryption e = new Encryption();
		String url = "http://cs346.cs.uwosh.edu/students/gustrd63/Project3/scoreUpload.php?username=" + username + "&quizname=" + quizname + "&totalQ=" + total;
		AsyncMethod<Encryption> method = new AsyncMethod<Encryption>(Encryption.class, e, "uploadScore");
		method.execute(url, score);
		method.getResult();
		
	}
	
	
}
